<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8">
  <title>管理界面</title>
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link href="//unpkg.com/layui@2.10.3/dist/css/layui.css" rel="stylesheet">
</head>
<style>
  td>img {
    width: 150px;
    height: 140px;
    /* height: 100%; */
    /* margin-left: 25%; */
  }

  .layui-layout-left input {
    margin-left: 100px;
    height: 25px;
    padding-left: 10px;
    position: relative;
    top: 5px;
  }

  td {
    width: 170px;
    text-align: center;
    padding: 10px 0;
  }

  button {
    margin: 5px 10px 0;
    padding: 8px;
  }

  .add_add,
  .xiu_show {
    border-radius: 10px;
    border: 1px solid black;
    width: 400px;
    padding: 80px 20px 50px;
    background-color: white;
    position: fixed;
    top: 200px;
    left: 520px;
    display: none;
  }

  .add_add>h5 {
    position: absolute;
    left: 170px;
    top: 20px;
  }

  .add_add>span,
  .xiu_show>span {
    margin: 0 20px 0 50px;
  }

  .add_add>button,
  .xiu_show>button {
    border-radius: 5px;
    border: 1px solid snow;
    margin-left: 70px;
    background-color: #435ac6;
    color: #cbaedf;
  }

  td>button {
    background-color: #cbaedf;
    color: yellow;
    border-radius: 5px;
  }

  .xiangqing {
    position: fixed;
    top: 100px;
    left: 500px;
    padding: 10px;
    background-color: pink;
    display: none;
  }

  .xiangqing img {
    width: 200px;
    height: 210px;
  }

  .xiu_show>h5 {
    margin: 20px 0 0;
    text-align: center;
  }
</style>

<body>
  <div class="layui-layout layui-layout-admin">
    <div class="layui-header">
      <div class="layui-logo layui-hide-xs layui-bg-black">商品管理系统</div>
      <!-- 头部区域（可配合layui 已有的水平导航） -->
      <ul class="layui-nav layui-layout-left">
        <!-- 移动端显示 -->
        <li class="layui-nav-item layui-show-xs-inline-block layui-hide-sm" lay-header-event="menuLeft">
          <i class="layui-icon layui-icon-spread-left"></i>
        </li>
        <li class="layui-nav-item layui-hide-xs"><a href="javascript:;" onclick="add_show()">点击添加商品</a></li>
        <input type="text" placeholder="输入商品名称进行搜索">
        <li class="layui-nav-item layui-hide-xs"><a href="javascript:;" onclick="includes()">搜索</a></li>
        <li class="layui-nav-item layui-hide-xs"><a href="javascript:;" onclick="sort_big()">价格升序</a></li>
        <li class="layui-nav-item layui-hide-xs"><a href="javascript:;" onclick="sort_small()">价格降序</a></li>
      </ul>
      <ul class="layui-nav layui-layout-right">
        <li class="layui-nav-item layui-hide layui-show-sm-inline-block">
          <a href="javascript:;">
            <img src="//unpkg.com/outeres@0.0.10/img/layui/icon-v2.png" class="layui-nav-img">
            我的
          </a>
          <dl class="layui-nav-child">
            <dd><a href="javascript:;">我的账户</a></dd>
            <dd><a href="javascript:;">更换账户</a></dd>
            <dd><a href="javascript:;">退出登录</a></dd>
          </dl>
        </li>
        <li class="layui-nav-item" lay-header-event="menuRight" lay-unselect>
          <a href="javascript:;">
            <i class="layui-icon layui-icon-more-vertical"></i>
          </a>
        </li>
      </ul>
    </div>
    <div class="layui-side layui-bg-black">
      <div class="layui-side-scroll">
        <!-- 左侧导航区域（可配合layui已有的垂直导航） -->
        <ul class="layui-nav layui-nav-tree" lay-filter="test">
          <li class="layui-nav-item"><a href="javascript:;" onclick="allin()">全部商品</a></li>
          <li class="layui-nav-item layui-nav-itemed">
            <a class="" href="javascript:;">数据统计</a>
            <dl class="layui-nav-child">
              <dd><a href="javascript:;">已售情况</a></dd>
              <dd><a href="javascript:;">库存占比</a></dd>
            </dl>
          </li>
        </ul>
      </div>
    </div>
    <div class="layui-body">
      <!-- 内容主体区域 -->
      <div style="padding: 15px;">
        <blockquote class="layui-elem-quote layui-text">
          <input type="number" placeholder="输入每页展示的个数" class="page">
        </blockquote>
        <div class="layui-card layui-panel">
          <!-- <div class="layui-card-header">
            <h3>商品列表</h3>
          </div> -->
          <div class="layui-card-body">
            <!-- <table border="1px">
              <thead>
                <tr>
                  <th>商品编号</th>
                  <th>名称</th>
                  <th>价格</th>
                  <th>图片</th>
                  <th>状态</th>
                  <th>库存</th>
                  <th>已售</th>
                  <th>操作</th>
                </tr>
              </thead>
              <tbody>

              </tbody>
            </table> -->
          </div>
        </div>
        <div class="add_add">
          <h5>添加商品</h5>
          <span>商品名称</span><input type="text"><br><br>
          <span>商品图片</span><input type="text" placeholder="请输入图片链接"><br><br>
          <span>商品价格</span><input type="text"><br><br>
          <span>商品状态</span><input type="text" placeholder="输入1（上架）或2（下架）"><br><br>
          <span>商品库存</span><input type="text"><br><br>
          <span>商品已售</span><input type="text"><br><br>
          <button onclick="add_goods()">确认添加商品</button><button onclick="no()">取消</button>
        </div>
        <div class="xiu_show">
          <h5>修改商品信息</h5>
          <span>商品名称</span><input type="text"><br><br>
          <span>商品图片</span><input type="text" placeholder="请输入图片链接"><br><br>
          <span>商品价格</span><input type="text"><br><br>
          <span>商品状态</span><input type="text" placeholder="输入1（上架）或2（下架）"><br><br>
          <span>商品库存</span><input type="text"><br><br>
          <span>商品已售</span><input type="text"><br><br>
          <button onclick="xiu_goods()">确认修改</button><button onclick="xiu_no()">取消</button>
        </div>
        <div class="xiangqing"></div>
        <br><br>
      </div>
    </div>
    <div class="layui-footer">
      <!-- 底部固定区域 -->
      <!-- 底部固定区域 -->
    </div>
  </div>

  <script src="https://code.jquery.com/jquery-3.7.1.js"></script>
  <script src="//unpkg.com/layui@2.10.3/dist/layui.js"></script>
  <!-- <script>
    //JS 
    layui.use(['element', 'layer', 'util'], function () {
      var element = layui.element;
      var layer = layui.layer;
      var util = layui.util;
      var $ = layui.$;

      //头部事件
      util.event('lay-header-event', {
        menuLeft: function (othis) { // 左侧菜单事件
          layer.msg('展开左侧菜单的操作', { icon: 0 });
        },
        menuRight: function () {  // 右侧菜单事件
          layer.open({
            type: 1,
            title: '更多',
            content: '<div style="padding: 15px;">看牛魔呢你，这啥也没有</div>',
            area: ['260px', '100%'],
            offset: 'rt', // 右上角
            anim: 'slideLeft', // 从右侧抽屉滑出
            shadeClose: true,
            scrollbar: false
          });
        }
      });
    });

    //分页
    var ge

    var num

    $.ajax({
      url: "https://liu.zzgoodqc.cn/goodsx/goodslist",
      type: "get",
      dataType: "json",
      success: (a) => {
        $(".page").on("blur", () => {
          $(".layui-footer").html("")
          ge = $(".page").val()
          num = Math.ceil(a.data.length / ge)
          for (let i = 1; i <= num; i++) {
            $(".layui-footer").append(`<button onclick="get_page(${i})">${i}</button>`)
          }
        })
      },
      error: (err) => {
        console.log(err);
      }
    })
    $(".layui-footer").on("click",()=>{
      $(event.target).css({
        "background-color":"blue",
        "color":"red"
      })
      $(event.target).siblings().css({
         "background-color":"white",
        "color":"black"
      })
    })
    function get_page(id) {
      $.ajax({
        url: `https://liu.zzgoodqc.cn/goodsx/listByPage?page=${id}&limit=${ge}`,
        type: "get",
        dataType: "json",
        success: (a) => {
          $("tbody").html("")
          a.data.forEach(item => {
            $("tbody").append(`
            <tr>
                  <td>${item.id}</td>
                  <td>${item.goods_name}</td>
                  <td>${item.price}</td>
                  <td><img src="${item.img}"></td>
                  <td>${item.status == 1 ? '上架' : item.status == 2 ? "下架" : "未知"}</td>
                  <td>${item.stock}</td>
                  <td>${item.sold}</td>
                  <td><button onclick="del(${item.id})">点击删除商品</button><br>
                    <button onclick="updata(${item.id},${item.status})">更新商品状态</button><br>
                    <button onclick="xiang(${item.id})">查看商品详情</button><br>
                    <button onclick="show(${item.id})">修改商品信息</button></td>
                </tr>`)
          })
        },
        error: (err) => {
          console.log(err);
        }
      })
    }

    //渲染
    function fn() {
      $.ajax({
        url: `https://liu.zzgoodqc.cn/goodsx/goodslist`,
        type: "get",
        dataType: "json",
        success: (a) => {
          $("tbody").html("")
          a.data.forEach(element => {
            $("tbody").append(`
          <tr>
                  <td>${element.id}</td>
                  <td>${element.goods_name}</td>
                  <td>${element.price}</td>
                  <td><img src="${element.img}"></td>
                  <td>${element.status == 1 ? "上架" : element.status == 2 ? "下架" : "未知"}</td>
                  <td>${element.stock}</td>
                  <td>${element.sold}</td>
                  <td><button onclick="del(${element.id})">点击删除商品</button><br>
                    <button onclick="updata(${element.id},${element.status})">更新商品状态</button><br>
                    <button onclick="xiang(${element.id})">查看商品详情</button><br>
                    <button onclick="show(${element.id})">修改商品信息</button></td>
                </tr>`)
          });
        },
        error: (err) => {
          console.log(err);
        }
      })
    }
    fn()

    //全部商品列表
    function allin() {
      fn()
      $("input").val("")
    }

    //添加
    function add_show() {
      $(".add_add").css("display", "block")
    }

    function no() {
      $(".add_add>input").val(""),
        $(".add_add").css("display", "none")
    }

    function add_goods() {
      $.ajax({
        url: "https://liu.zzgoodqc.cn/goodsx/add",
        type: "post",
        data: {
          goods_name: $(".add_add input:eq(0)").val(),
          img: $(".add_add input:eq(1)").val(),
          price: $(".add_add input:eq(2)").val(),
          status: $(".add_add input:eq(3)").val(),
          stock: $(".add_add input:eq(4)").val(),
          sold: $(".add_add input:eq(5)").val()
        },
        dataType: "json",
        success: () => {
          fn()
          $(".add_add>input").val("")
        },
        error: (err) => {
          console.log(err);
        }
      })
    }



    //删除
    function del(id) {
      $.ajax({
        url: "https://liu.zzgoodqc.cn/goodsx/delete?id=" + id,
        type: "get",
        dataType: "json",
        success: (a) => {
          fn()
        },
        error: (err) => {
          console.log(err);
        }
      })
    }

    // //排序
    // function sort_big(){
    //   $.ajax({
    //     url:"https://liu.zzgoodqc.cn/goodsx/sort",
    //     type:"get",
    //     data:{
    //       sort:asc,
    //       type:price,
    //       page:1,
    //       limit:10
    //     },
    //     dataType:"json",
    //     success:()=>{
    //       fn()
    //     },
    //     error:(err)=>{
    //       console.log(err);
    //     }
    //   })
    // }

    //搜索
    function includes() {
      $.ajax({
        url: "https://liu.zzgoodqc.cn/goodsx/search?goods_name=" + $("input").eq(0).val(),
        type: "post",
        dataType: "json",
        success: (a) => {
          a.data.forEach(item => {
            $("tbody").html(`
            <tr>
                  <td>${item.id}</td>
                  <td>${item.goods_name}</td>
                  <td>${item.price}</td>
                  <td><img src="${item.img}"></td>
                  <td>${item.status == 1 ? '上架' : item.status == 2 ? "下架" : "未知"}</td>
                  <td>${item.stock}</td>
                  <td>${item.sold}</td>
                  <td><button onclick="del(${item.id})">点击删除商品</button><br>
                    <button onclick="updata(${item.id},${item.status})">更新商品状态</button><br>
                    <button onclick="xiang(${item.id})">查看商品详情</button><br>
                    <button onclick="show(${item.id})">修改商品信息</button></td>
                </tr>`)
          })
        },
        error: (err) => {
          console.log(err);
        }
      })
    }

    //显示修改框
    var id
    function show(i) {
      $(".xiu_show").css("display", "block")
      id = i
    }

    //修改信息
    function xiu_goods() {
      $.ajax({
        url: "https://liu.zzgoodqc.cn/goodsx/update",
        type: "post",
        data: {
          id,
          goods_name: $(".xiu_show input:eq(0)").val(),
          img: $(".xiu_show input:eq(1)").val(),
          price: $(".xiu_show input:eq(2)").val(),
          status: $(".xiu_show input:eq(3)").val(),
          stock: $(".xiu_show input:eq(4)").val(),
          sold: $(".xiu_show input:eq(5)").val()
        },
        dataType: "json",
        success: () => {
          fn()
          $(".xiu_show>input").val("")
          $(".xiu_show").css("display", "none")
        },
        error: (err) => {
          console.log(err);
        }
      })
    }

    //取消框
    function xiu_no() {
      $(".xiu_show>input").val("")
      $(".xiu_show").css("display", "none")
    }

    //更新状态
    function updata(id, goods_status) {
      if (goods_status == 1 && "上架") {
        goods_status = 2
      } else if (goods_status == 2 && "下架") {
        goods_status = 1
      }
      $.ajax({
        url: `https://liu.zzgoodqc.cn/goodsx/updateStatus?id=${id}&status=${goods_status}`,
        type: "get",
        dataType: "json",
        success: () => {
          fn()
        },
        error: (err) => {
          console.log(err);
        }
      })
    }

    //详情
    function xiang(id) {
      $.ajax({
        url: "https://liu.zzgoodqc.cn/goodsx/detail?id=" + id,
        type: "get",
        dataType: "json",
        success: (a) => {
          $('.xiangqing').css("display","block")
          $('.xiangqing').html(`
                    <h3>商品名称</h3><li>${a.data.goods_name}</li>
                    <h3>商品图片</h3><li><img src="${a.data.img}"></li>
                    <h3>商品价格</h3><li>${a.data.price}</li>
                    <h3>商品库存</h3><li>${a.data.stock}</li>
                    <h3>商品状态</h3><li>${a.data.status}</li>
                    <h3>商品已售</h3><li>${a.data.sold}</li>
                    <button onclick="que()">关闭</button>`)
        },
        error: (err) => {
          console.log(err);
        }
      })
    }

    function que() {
      $('.xiangqing').css("display", "none")
    }

  </script> -->
</body>

</html>